package com.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.demo.common.BaseController;
import com.demo.common.R;
import com.demo.common.TestDTO;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 测试swagger控制器
 * @Author Dang Xianyue
 * @Date 2020-08-09 23:19
 * @Version 1.0
 */
@Slf4j
@Api(tags = "测试swagger接口")
@RestController
@RequestMapping("/test")
public class TestSwaggerController extends BaseController {

    @ApiOperation(value = "test1接口",httpMethod = "POST")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id",value = "编号",paramType = "form",dataType = "Integer",required = true),
        @ApiImplicitParam(name = "name",value = "名称",paramType = "form",dataType = "String",required = true),
        @ApiImplicitParam(name = "flag",value = "标记",paramType = "form",dataType = "Boolean",required = true)
    })
    @RequestMapping(value = "/test1",method = RequestMethod.POST)
    public R test1(@RequestParam(value = "id")Integer id,
                   @RequestParam(value = "name")String name,
                   @RequestParam(value = "flag")Boolean flag){
        JSONObject jo = new JSONObject();
        jo.put("id",id);
        jo.put("name",name);
        jo.put("flag",flag);
        log.info("test1接口参数：{}", JSON.toJSONString(jo));
        return ok(jo);
    }


    @ApiOperation(value = "test2接口",httpMethod = "POST")
    @RequestMapping(value = "/test2",method = RequestMethod.POST)
    public R<TestDTO> test2() throws Exception{
        return ok(new TestDTO());
    }

    @ApiOperation(value = "test3接口",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "ids说明。。。。。。", required = false)
    })
    @RequestMapping(value = "/test3",method = RequestMethod.POST)
    public R<List<TestDTO>> test3(@RequestParam(value = "ids")Integer[] ids) throws Exception{
        System.out.println(ids);
        List<TestDTO> list = new ArrayList<TestDTO>(){
            {
                for (int i = 0; i < ids.length; i++) {
                    add(new TestDTO().setId(ids[i]));
                }
            }
        };
        return ok(list);
    }

}
